package com.yanqu.road.server.manger.activity.hanghai;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.HangHaiActivityBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.hanghai.HangHaiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HangHaiMgr extends TempMgr {

    private static HangHaiConfig hangHaiConfig;

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload HangHaiMgr start!");
        // 获取活动
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.HangHaiActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no HangHai Activity in show time.");
            return;
        }
        ActivityInfo activityInfo = openActivityInfoList.get(0);
        int activityId = activityInfo.getActivityId();

        // 加载配置
        HangHaiConfig tmpConfig = new HangHaiConfig();
        tmpConfig.setActivityId(activityId);
        tmpConfig.setActivityInfo(activityInfo);

        // 货物
        tmpConfig.setGoodsConfigMap(HangHaiActivityBusiness.getHangHaiGoodsConfigMap(activityId));
        // 港口
        tmpConfig.setPortConfigMap(HangHaiActivityBusiness.getHangHaiPortConfigMap(activityId));
        // 船
        tmpConfig.setShipConfigList(HangHaiActivityBusiness.getHangHaiShipConfigMap(activityId));

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityId);

        List<Integer> operationTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_OPERATION_TIME_PARAM.getName()).getIntListValue(";");
        tmpConfig.setStartOperationTime(operationTimeParam.get(0) * 60L * 1000L);
        tmpConfig.setEndOperationTime(operationTimeParam.get(1) * 60L * 1000L);

        tmpConfig.setBaseMoney(activityConfigMap.get(eActivityConfigType.HANGHAI_BASE_MONEY.getName()).getIntValue());

        tmpConfig.setTitleMoneyAddition(activityConfigMap.get(eActivityConfigType.HANGHAI_TITLE_MONEY_ADDITION.getName()).getIntValue());

        List<Integer> refreshTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REFRESH_TIME_LIST.getName()).getIntListValue(";");
        List<Long> refreshTimeList = new ArrayList<>();
        for (Integer refreshTime : refreshTimeParam) {
            refreshTimeList.add(refreshTime * 60L * 1000L);
        }
        tmpConfig.setRefreshTimeList(refreshTimeList);

        List<Integer> refreshStockTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REFRESH_STOCK_TIME_LIST.getName()).getIntListValue(";");
        List<Long> refreshStockTimeList = new ArrayList<>();
        for (Integer refreshTime : refreshStockTimeParam) {
            refreshStockTimeList.add(refreshTime * 60L * 1000L);
        }
        tmpConfig.setRefreshStockTimeList(refreshStockTimeList);

        tmpConfig.setMoveTime(activityConfigMap.get(eActivityConfigType.HANGHAI_MOVE_TIME.getName()).getLongValue() * 60L * 1000L);

        tmpConfig.setSpecStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEC_STOCK_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setCommonStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_COMMON_STOCK_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setLackStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_LACK_COUNT_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setSpecPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEC_PRICE_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setCommonPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_COMMON_PRICE_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setLackPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_LACK_PRICE_PARAM.getName()).getIntListValue(";"));

        tmpConfig.setSpeedUpItemId(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEED_UP_ITEM_ID.getName()).getIntValue());

        tmpConfig.setMoneyItemId(activityConfigMap.get(eActivityConfigType.HANGHAI_MONEY_ITEM_ID.getName()).getIntValue());

        String reportRewardParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REPORT_REWARD_PARAM.getName()).getValue();
        for (String param : StringUtils.stringToStringList(reportRewardParam, "\\|")) {
            List<String> paramList = StringUtils.stringToStringList(param, ",");
            tmpConfig.getReportTimesList().add(Integer.valueOf(paramList.get(0)));
            tmpConfig.getReportRewardList().add(paramList.get(1));
        }

        tmpConfig.setUnionMinMoneyParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_UNION_MIN_MONEY_PARAM.getName()).getIntListValue(";"));

        hangHaiConfig = tmpConfig;
        getLogger().info("reload HangHaiMgr finish!");

        // 同步数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getCopyPlayerList()) {
            gamePlayer.getModule(HangHaiModule.class).loginHangHai();
        }
    }

    public static HangHaiConfig getHangHaiConfig() {
        return hangHaiConfig;
    }

    public static boolean isInOperationTime(HangHaiConfig config) {
        if (!ActivityMgr.activityInTime(config.getActivityInfo())) {
            return false;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        long now = System.currentTimeMillis();
        return zeroTime + config.getStartOperationTime() < now
                && zeroTime + config.getEndOperationTime() > now;
    }

    /**
     * 商会能否变化人员
     */
    public static boolean canChangeUnion() {
        if (hangHaiConfig != null) {
            return !ActivityMgr.activityInTime(hangHaiConfig.getActivityInfo());
        }
        return true;
    }

    /**
     * 检查分组
     */
    public static int checkGroup(long userId) {
        if (hangHaiConfig == null) {
            return GameErrorCode.E_UNION_ACTIVITY_GROUP_ACTIVITY_END;
        }
        int activityId = hangHaiConfig.getActivityInfo().getActivityId();

        // 拿分组ID
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userId);

        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            if (GameUnionActivityGroupMgr.haveGroup(activityId)) {
                return GameErrorCode.E_UNION_ACTIVITY_GROUP_NO_ABLE;
            } else {
                return GameErrorCode.E_UNION_ACTIVITY_GROUP_WAIT_GROUP;
            }
        }
        return 0;
    }

}
